package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.team.model.AddTeamModel;
import com.ls.controller.team.model.EditTeamModel;
import com.ls.controller.team.model.QueryTeamModel;
import com.ls.entity.*;
import com.ls.mapper.TeamMapper;
import com.ls.service.ICustomerService;
import com.ls.service.ILinkmanService;
import com.ls.service.ITeamService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.ls.constant.SystemConstants.VALID_FALSE;
import static com.ls.constant.SystemConstants.VALID_TRUE;

/**
 * <p>
 * 分组表，与customer关联 服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2024-11-20
 */
@RequiredArgsConstructor
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {
    private final ILinkmanService linkmanService;

    @Override
    public ResultData pageList(QueryTeamModel model) {
        Long customerId=null;
        if (model.getCustomerId()==null){
            Customer customer = SaTokenUtilPy.getCustomer();
            customerId=customer.getCustomerId();
        }else{
            customerId=model.getCustomerId();
        }
        //1 查询出分组列表并包含成员数量
        List<Team> teamList = baseMapper.selectTeamListWithMemberCount(
                customerId, 
                StringUtils.isNotBlank(model.getTeamName()) ? model.getTeamName() : null, 
                model.getTeamType());
        return ResultData.successData(teamList);
    }

    @Override
    public ResultData add(AddTeamModel model) {
        Customer customer = SaTokenUtilPy.getCustomer();
        Long customerId = customer.getCustomerId();
        //根据用户id查询用户企业
        Team team1 = getOne(new LambdaQueryWrapper<Team>()
                .eq(Team::getCustomerId, customerId)
                .eq(Team::getValidFlag, VALID_TRUE)
                .eq(Team::getTeamType, model.getTeamType())
                .eq(Team::getTeamName, model.getTeamName()));
        if (team1 != null) {
            return ResultData.failure("当前用户已存在该分组");
        }
        Team team = new Team();
        BeanUtils.copyProperties(model, team);
        team.setCustomerId(customerId);
        team.setCreatorId(customerId);
        team.setUpdaterId(customerId);
        team.setUpdateTime(LocalDateTime.now());
        team.setCreateTime(LocalDateTime.now());
        team.setValidFlag(VALID_TRUE);
        boolean flag = save(team);
        if (flag) {
            return ResultData.success();
        } else {
            return ResultData.failure("内部服务器错误");
        }
    }
    @Override
    public ResultData edit(EditTeamModel model) {
        Customer customer = SaTokenUtilPy.getCustomer();
        Long customerId = customer.getCustomerId();
        //判断当前用户是否存在重复的分组
        Team team1 = getOne(new LambdaQueryWrapper<Team>()
                .eq(Team::getCustomerId, customerId)
                .eq(Team::getValidFlag, VALID_TRUE)
                .eq(Team::getTeamType, model.getTeamType())
                .eq(Team::getTeamName, model.getTeamName())
                .ne(Team::getTeamId, model.getTeamId())
        );
        if (team1 != null) {
            return ResultData.failure("当前用户已存在该分组！");
        }
        Team team = getById(model.getTeamId());
        BeanUtils.copyProperties(model, team);
        team.setUpdateTime(LocalDateTime.now());
        team.setUpdaterId(customerId);
        Boolean flag = saveOrUpdate(team);
        if (flag) {
            return ResultData.successMsg("更新成功");
        } else {
            return ResultData.failure("更新失败");
        }
    }

    @Override
    public ResultData del(Integer teamId) {
        Team team = getById(teamId);
        if (team == null) {
            return ResultData.failure("分组名称不存在，无需删除");
        }
        //判断是否被使用
        int count = linkmanService.count(new LambdaQueryWrapper<Linkman>().eq(Linkman::getTeamId, teamId));
        if (count > 0) {
            return ResultData.failure("分组名称已经被引用，不能删除");
        }
        team.setValidFlag(VALID_FALSE);
        boolean flag = saveOrUpdate(team);
        if (flag) {
            return ResultData.success();
        } else {
            return ResultData.failure("内部服务器错误");
        }
    }
}
